package com.gxa.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.mapper.OrderMapper;
import com.gxa.param.OrderParam;
import com.gxa.pojo.Contract;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Goods;
import com.gxa.pojo.Order;
import com.gxa.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Throwable.class)
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;



    @Override
    public LayR list(OrderParam param) {

        PageHelper.startPage(param.getPage(), param.getLimit());
        List<Order> orders = orderMapper.findAll(param);
        // 封装pageInfo
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        List<Order> list = pageInfo.getList();
        for (Order order : list) {

            System.out.println(order);
        }

        return new LayR(0, "success", pageInfo.getTotal(), pageInfo.getList());
    }


    @Override
    public R deleteAll(String[] ids) {
        orderMapper.deleteAll(ids);
        return new R(200, "删除成功！");
    }


    @Override
    public LayR customerList(OrderParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<Order> orders = orderMapper.findAll(param);
        List<Integer> cusIds=orderMapper.findCusId();
        List<Order> orders2=new ArrayList<>();
        for (Integer cusId: cusIds){
            for (Order order:orders){
                if(cusId.equals(order.getCustomerId())){
                    orders2.add(order);
                    break;
                }
            }
        }
        for (Order order : orders2) {
            order.setTotal(orderMapper.findTotal(order.getCustomerId()));
            System.out.println(order);
        }
        System.out.println("========---"+orders2.toString());
        // 封装pageInfo
        PageInfo<Order> pageInfo = new PageInfo<>(orders2);
        List<Order> list = pageInfo.getList();

        return new LayR(0, "success", pageInfo.getTotal(), pageInfo.getList());
    }


    @Override
    public Order findById(Integer id) {
        return orderMapper.findById(id);
    }


    @Override
    public LayR list() {
        List<Goods> goods = orderMapper.findAllGoods();
        return new LayR(200, "success", goods);
    }


    @Override
    public LayR add(Order order) {
        Timestamp cur_time=new Timestamp(System.currentTimeMillis());
        //获取当前时间

        order.setOrderTime(cur_time);
        order.setStatus(2);
        String  uuid = UUID.randomUUID().toString();
        order.setNumber(uuid);
        String[] sourceArray = order.getCommodity().split("/");
        order.setCommodity(sourceArray[0]);

        BigDecimal bd = null;
        if(sourceArray[1]!=null){
            bd=new BigDecimal(sourceArray[1]);
        }
        BigDecimal temp = new BigDecimal(order.getNums());
        order.setMount(bd.multiply(temp));
        order.setIsDeleted(0);

        int res=orderMapper.add(order);
        if(res==1) {
            return new LayR(200, "下单成功！", order);
        }
        else {
            return null;
        }

    }
}
